package cn.wikiflyer.beikang.act.user;

import java.lang.reflect.Type;

import net.tsz.afinal.annotation.view.ViewInject;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
import cn.wikiflyer.beikang.BaseActivity;
import cn.wikiflyer.beikang.R;
import cn.wikiflyer.beikang.act.UserInfoBean;
import cn.wikiflyer.beikang.act.user.bean.InComeOrderListBean;
import cn.wikiflyer.beikang.act.user.bean.InComeOrderListBean.InComeOrderBean;
import cn.wikiflyer.beikang.bean.OrderListBean.OrderBean;
import cn.wikiflyer.beikang.bean.base.BKBaseBean;
import cn.wk.libs4a.view.WKHeader;
import cn.wk.libs4a.view.list.WKListViewInterface;
import cn.wk.libs4a.view.list.WKListViewUtils;
import cn.wk.libs4a.view.list.WKViewHolder;
import cn.wk.libs4a.view.pulltorefresh.PullToRefreshListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
 * 收入订单
 * 
 * @author qunlee_mr
 */
public class UserInComeOrderListAct extends BaseActivity implements
		WKListViewInterface, OnItemClickListener {

	@ViewInject(id = R.id.header)
	private WKHeader header;

	private WKListViewUtils<InComeOrderBean, InComeOrderListBean> listUtils;
	private UserInfoBean mUserBean;

	@Override
	public void getIntentData(Bundle savedInstanceState) {
		super.getIntentData(savedInstanceState);
		mUserBean = (UserInfoBean) getIntent().getSerializableExtra("userbean");
	}

	@Override
	public void loadXml() {
		setContentView(R.layout.bk_income_order_list_layout);// common_layout布局作为列表通用布局
	}

	@Override
	public void init() {
		header.setTitle("收入订单");
		header.setLeftAsBack();
		initList();
	}

	@Override
	public void setData() {
		listUtils.updateData();
	}

	private void initList() {
		PullToRefreshListView pList = (PullToRefreshListView) findViewById(R.id.wk_pull_refresh_list);

		View view_header = View.inflate(ctx, R.layout.header_balanceorder_list,
				null);
		((TextView) view_header.findViewById(R.id.center_cash)).setText(""
				+ mUserBean.history_cash);
		((TextView) view_header.findViewById(R.id.center_balance)).setText(""
				+ mUserBean.money);
		((TextView) view_header.findViewById(R.id.center_unclear)).setText(""
				+ mUserBean.amount);

		listUtils = new WKListViewUtils<InComeOrderBean, InComeOrderListBean>() {
		};
		listUtils.init(ctx, this, pList, view_header);
		listUtils.pullRefreshListView.setOnItemClickListener(this);
	}

	@Override
	public String getData(boolean... isLocal) {
		return app().net.getInComeList(listUtils.page, listUtils.pageCount,
				isLocal);
	}

	@Override
	public void getDataBack(String result, boolean isLocal) {
		Type cvbType = new TypeToken<BKBaseBean<InComeOrderListBean>>() {
		}.getType();
		Gson gson = new Gson();
		listUtils.bean = gson.fromJson(result, cvbType);
		// 使用数据
		listUtils.list.addAll(listUtils.bean.data.list);

		listUtils.adapter.notifyDataSetChanged();
	}

	@Override
	public WKViewHolder createViewHolder(View arg1) {
		return new MyViewHolder(arg1);
	}

	@Override
	public int getItemViewId() {
		return R.layout.act_user_balanceorder_list_item;
	}

	class MyViewHolder extends WKViewHolder<InComeOrderBean> {

		TextView order_code;
		TextView order_customer;
		TextView order_count;
		TextView order_type;
		TextView order_status;
		TextView order_prize;

		public MyViewHolder(View view) {
			order_code = (TextView) view
					.findViewById(R.id.balanceorderitem_code);
			order_customer = (TextView) view
					.findViewById(R.id.balanceorderitem_customername);
			order_count = (TextView) view
					.findViewById(R.id.balanceorderitem_count);
			order_type = (TextView) view
					.findViewById(R.id.balanceorderitem_type);
			order_status = (TextView) view
					.findViewById(R.id.balanceorderitem_status);
			order_prize = (TextView) view
					.findViewById(R.id.balanceorderitem_prize);
		}

		@Override
		public void setData(InComeOrderBean t) {
			order_code.setText("" + t.orderno);
			order_customer.setText("" + t.buyer);
			order_count.setText(t.quantity + "盒");
			order_prize.setText("￥" + t.money);

			order_type.setText("" + t.type);

			String[] status = { "未结算", "已结算" };
			order_status.setText(status[0]);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
	}

}
